/*     */ package com.zimbra.cs.gal;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import java.text.ParseException;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GalSyncToken
/*     */ {
/*     */   static final String LDAP_GENERALIZED_TIME_FORMAT = "yyyyMMddHHmmss'Z'";
/*     */   private String mLdapTimestamp;
/*     */   private HashMap<String, String> mChangeIdMap;
/*     */   
/*     */   public GalSyncToken(String token)
/*     */   {
/*  31 */     parse(token);
/*     */   }
/*     */   
/*     */   public GalSyncToken(String ldapTs, String accountId, int changeId) {
/*  35 */     this.mLdapTimestamp = ldapTs;
/*  36 */     this.mChangeIdMap = new HashMap();
/*  37 */     this.mChangeIdMap.put(accountId, "" + changeId);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private void parse(String token)
/*     */   {
/*  45 */     this.mChangeIdMap = new HashMap();
/*  46 */     int pos = token.indexOf(':');
/*  47 */     if (pos == -1)
/*     */     {
/*  49 */       this.mLdapTimestamp = token;
/*  50 */       return;
/*     */     }
/*  52 */     this.mLdapTimestamp = token.substring(0, pos);
/*  53 */     boolean finished = false;
/*  54 */     while (!finished) {
/*  55 */       token = token.substring(pos + 1);
/*  56 */       int sep = token.indexOf(':');
/*  57 */       if (sep == -1)
/*  58 */         return;
/*  59 */       String key = token.substring(0, sep);
/*  60 */       String value = null;
/*  61 */       pos = token.indexOf(':', sep + 1);
/*  62 */       if (pos == -1) {
/*  63 */         finished = true;
/*  64 */         value = token.substring(sep + 1);
/*     */       } else {
/*  66 */         value = token.substring(sep + 1, pos);
/*     */       }
/*  68 */       this.mChangeIdMap.put(key, value);
/*     */     }
/*     */   }
/*     */   
/*     */   public String getLdapTimestamp() {
/*  73 */     return this.mLdapTimestamp;
/*     */   }
/*     */   
/*     */   public String getLdapTimestamp(String format) throws ServiceException
/*     */   {
/*  78 */     SimpleDateFormat standardFormat = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
/*  79 */     SimpleDateFormat fmt = new SimpleDateFormat(format);
/*     */     try {
/*  81 */       Date ts = standardFormat.parse(this.mLdapTimestamp);
/*  82 */       return fmt.format(ts);
/*     */     }
/*     */     catch (ParseException e) {}
/*     */     
/*  86 */     return this.mLdapTimestamp;
/*     */   }
/*     */   
/*     */   public int getChangeId(String accountId)
/*     */   {
/*  91 */     String cid = (String)this.mChangeIdMap.get(accountId);
/*  92 */     if (cid != null)
/*  93 */       return Integer.parseInt(cid);
/*  94 */     return 0;
/*     */   }
/*     */   
/*     */   public boolean doMailboxSync() {
/*  98 */     return (this.mLdapTimestamp.length() == 0) || (this.mChangeIdMap.size() > 0);
/*     */   }
/*     */   
/*     */   public boolean isEmpty() {
/* 102 */     return (this.mLdapTimestamp.length() == 0) && (this.mChangeIdMap.size() == 0);
/*     */   }
/*     */   
/*     */   public void merge(GalSyncToken that) {
/* 106 */     ZimbraLog.gal.debug("merging token " + this + " with " + that);
/* 107 */     this.mLdapTimestamp = LdapUtil.getEarlierTimestamp(this.mLdapTimestamp, that.mLdapTimestamp);
/* 108 */     for (String aid : that.mChangeIdMap.keySet())
/* 109 */       this.mChangeIdMap.put(aid, that.mChangeIdMap.get(aid));
/* 110 */     ZimbraLog.gal.debug("result: " + this);
/*     */   }
/*     */   
/*     */   public String toString() {
/* 114 */     StringBuilder buf = new StringBuilder(this.mLdapTimestamp);
/* 115 */     for (String aid : this.mChangeIdMap.keySet())
/* 116 */       buf.append(":").append(aid).append(":").append((String)this.mChangeIdMap.get(aid));
/* 117 */     return buf.toString();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/gal/GalSyncToken.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */